﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using Simhopp.Common;
using System.Windows;
using System.Windows.Input;
using System.Windows.Controls.Primitives;

namespace Simhopp.Client
{
    /// <summary>
    /// Interaction logic for InputBox.xaml
    /// </summary>
    public partial class InputBox : Window
    {
        public Collection<MutableKeyValuePair<String, Object>> ControlInputMap { get; set; }
        public String InfoText { get; set; }
        /// <summary>
        /// Creates a new inputbox for retrieving user input or displaying messages to the user.
        /// </summary>
        /// <param name="windowName">The title of the window</param>
        /// <param name="infoText">A help message for the user, displayed above the input textboxes</param>
        /// <param name="controlInputMap">List of input description and input type pairs. The first string will be a label for a textbox.</param>
        public InputBox(string windowName, string infoText, Collection<MutableKeyValuePair<string, object>> controlInputMap = null)
        {
            this.PreviewKeyDown += this.CheckKey;
            this.DataContext = this;
            this.ControlInputMap = controlInputMap;
            this.Title = windowName;
            this.InfoText = infoText;
            InitializeComponent();
            if (controlInputMap != null)
            {
                foreach (var mutableKeyValuePair in controlInputMap)
                {
                    var panel = new StackPanel();
                    panel.Orientation = Orientation.Horizontal;
                    var label = new Label();
                    label.Content = mutableKeyValuePair.Key;
                    label.Width = 100;

                    var textBox = new TextBox();
                    var myBinding = new Binding();
                    myBinding.Mode = BindingMode.TwoWay;
                    myBinding.Source = mutableKeyValuePair;
                    myBinding.Path = new PropertyPath("Value", null);
                    textBox.SetBinding(TextBox.TextProperty, myBinding);
                    textBox.Width = this.Width - label.Width;

                    panel.Children.Add(label);
                    panel.Children.Add(textBox);
                    StackPanel.Children.Add(panel);
                }
                (StackPanel.Children[0] as StackPanel).Children[1].Focus();
            }
            this.UpdateLayout();
            this.Show();
            this.WindowStartupLocation = WindowStartupLocation.Manual;
            this.Left = App.Current.MainWindow.Left + App.Current.MainWindow.ActualWidth / 2 - this.Width / 2;
            this.Top = App.Current.MainWindow.Top + App.Current.MainWindow.ActualHeight / 2 - this.ActualHeight / 2;
            this.Hide();
        }



        /// <summary>
        /// Handles all button clicks, sets the dialog Result (OK -> True) and then closes the window.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            var b = sender as Button;
            DialogResult = b != null && b.Equals(OkButton);
            Close();

        }
        //Allows enter to close the window.
        private void CheckKey(object sender, KeyEventArgs e)
        {
            if(e.Key == Key.Return)
                Button_Click_1(this.OkButton, e);
        }

    }
}
